package com.pphh.demo;

import com.pphh.demo.service.DemoService;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Please add description here.
 *
 * @author huangyinhuang
 * @date 2019/4/19
 */
public class AsyncServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AsyncContext asyncContext = request.startAsync();

        asyncContext.start(() -> {
            String msg = DemoService.run(1);
            try {
                asyncContext.getResponse().getWriter().write("[async servlet test] " + msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
            asyncContext.complete();
        });
    }
}
